const util = require("../../utils/util");

// pages/index/index.js
const app = getApp();
let timer = null;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    icon: { name: 'tag', isShow: true },
    cardCur: 0,
    swiperList: [{
      id: 0,
      type: 'image',
      url: 'https://cdn.pixabay.com/photo/2018/09/24/15/04/board-3700116__340.jpg'
    }, {
      id: 1,
      type: 'image',
        url: 'https://cdn.pixabay.com/photo/2015/09/02/12/26/desk-918425__340.jpg',
    }, {
      id: 2,
      type: 'image',
        url: 'https://cdn.pixabay.com/photo/2015/07/31/14/59/creative-869200__340.jpg'
    }, {
      id: 3,
      type: 'image',
        url: 'https://cdn.pixabay.com/photo/2019/03/12/20/27/kanban-4051777__340.jpg'
    }],
    jobList:[],
    page:1,
    page_size:6,
    isover:1,
    value:"123",
    nowDate: util.formatTime(new Date()),
    latitude:'', longitude:'', searchstr:''
  },
  // 跳转至工作详情
  job: function (e){
    try {
      var value = app.getCache('token')
      if (!value) {
        wx.showToast({
          title: '请到个人中心登录',
          icon: 'none'
        })
      }
      console.log(e.currentTarget.dataset.id)
      if(value){
        wx.navigateTo({
          url: '../commodity-detail/commodity-detail?id=' + e.currentTarget.dataset.id,
        })
      }
    } catch (e) {
      console.log(e)
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var _this = this;
    wx.getLocation({
      type: 'wgs84',
      success: (res)=>{
        const latitude = res.latitude
        const longitude = res.longitude
        _this.setData({
          latitude,
            longitude
        }, _this.initData)
      },
      fail:err=>{
        console.log(err)
        _this.initData()
      }
     })
  },
  DotStyle(e) {
    this.setData({
      DotStyle: e.detail.value
    })
  },
  // cardSwiper
  cardSwiper(e) {
    this.setData({
      cardCur: e.detail.current
    })
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    console.log("下拉刷新")
    var _this = this;
    wx.getLocation({
      type: 'wgs84',
      success: (res)=>{
        const latitude = res.latitude
        const longitude = res.longitude
        console.log(res)
        _this.setData({
          latitude,
            longitude
        }, _this.initData)
       
      },
      fail:()=>{

      }
     })
    wx.stopPullDownRefresh();
  },

  initData(){
    let { latitude, longitude, searchstr }  = this.data;
    const _this = this;
    wx.cloud.callFunction({
      // 云函数名称
      name: 'getJobList',
      // 传给云函数的参数
      data: {
        latitude,
        longitude,
        search :searchstr
      }
    })
    .then(res => {
      console.log(res.result) 
      let list = res.result.data.map(el=>{
        if(new Date() < new Date(el.endTime)){
          el.show=true;
        }
        return el
      })
      _this.setData({
        jobList: res.result.data||[]
      });
    })
    .catch(console.error)
  },

  // 搜索兼职
  search(e){
    let value = e.detail.value;
    // 防抖函数
    if(timer){
      clearTimeout(timer);
      timer = null;
    }
    this.setData({
      searchstr: value
    })
    timer = setTimeout(() => {
      console.log(this.data.searchstr);
      this.initData()
    }, 1000);
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
})